<?php
namespace Models;
use Path;

/**
 * @author Eugene Baranov <evgeny@baranov.eu>
 */
class MenuItem extends Base {
    protected $id;
    protected $name;
    protected $path;
    protected $position;
    protected $role = User::ROLE_GUEST;
    protected $ns;
    protected $controller;
    protected $action;
    protected $active = false;
    protected $param = array();

    public function getParam() {
        return $this->param;
    }

    /**
     * @var \Models\MenuItem[]
     */
    protected $menu = array();

    /**
     * @var \Models\MenuItem
     */
    protected $parent;
    protected $parent_id;

    /**
     * @return boolean
     */
    public function canAccess() {
        $role = ($u = \App::getCurrentUser()) ? $u->getMaxRole() : User::ROLE_GUEST;
        return ($this->role <= $role);
    }

    public function getName() {
        return $this->name;
    }

    /**
     * @return \Models\MenuItem
     */
    public function getParent() {
        return $this->parent;
    }

    public function getController() {
        $result = array();
        if ($this->parent) {
            $pc = $this->parent->getController();
            if ($pc) {
                $result[] = $pc;
            }
        }
        if ($this->ns) {
            $result[] = $this->ns;
        }
        if ($this->controller) {
            $result[] = $this->controller;
        }

        return implode(NS, $result);
    }

    public function getAction() {
        return $this->action;
    }

    public function getId() {
        return $this->id;
    }

    public function getPath() {
        $p = array();
        if ($this->parent) {
            $p = $this->parent->getPath();
        }
        $p[] = $this->path;
        return $p;
    }

    public function haveMenu() {
        return is_array($this->menu) && count($this->menu) > 0;
    }

    public function getMenu() {
        return $this->menu;
    }

    public function toArray() {
        return array_merge(parent::toArray(), array(
            'id' => $this->id,
            'name' => $this->name,
            'position' => $this->position,
            'path' => $this->path,
            'role' => $this->role,
            'controller' => $this->controller,
            'ns' => $this->ns,
            'action' => $this->action,
            'parent_id' => $this->parent ? $this->parent->getId() : null,
            'updated' => $this->updated,
            'created' => $this->created,
        ));
    }

    public function fromArray($data) {
        if (isset($data['menu']) && is_array($data['menu'])) {
            foreach ($data['menu'] as &$i) {
                $i = new self($i);
                $i->parent = $this;
            }
        }

        parent::fromArray($data);
    }

    public function isActive() {
        return $this->active;
    }

    /**
     * @return MenuItem $this
     */
    public function route() {
        $path = Path::getInstance();
        if ($path->current() == $this->path) {
            $this->active = true;
            $path->next();
            foreach ($this->getMenu() as $item) {
                if ($r = $item->route()) {
                    return $r;
                }
            }
            return $this;
        } else {
            return false;
        }
    }

    public function getClass() {
        $mc = array();
        if ($this->isActive()) {
            $mc[] = 'active';
        }
        if ($this->haveMenu()) {
            $mc[] = 'dropdown';
        }

        return implode(' ', $mc);
    }
}